home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_3a.arc / BTRIEVE.C next >
C/C++ Source or Header  |  1989-04-20  |  10KB  |  336 lines

  1. /* LISTING 1 */
  2.  
  3. /*   This Btrieve-required structure holds information about one key 
  4.  *   (or one segment of a key if the key is segmented). 
  5.  *   It needs to be defined before the file structure
  6.  *   since it will be included.
  7.  */
  8.  
  9.  
  10. struct keyspec         /* key segment structure specification */
  11. {     int  key_pos;    /* start position in record for key segment */
  12.       int  key_len;    /* length of key segment */
  13.       int  key_flag;   /* a vector of bit flags describing
  14.                           attributes of the key */
  15.       long key_tot;    /* unique key count (no dups, nulls)
  16.                           returned by STATUS operation */
  17.       char key_type;   /* a vector of bit flags used to define
  18.                           the data type */
  19.       char null_char;  /* define value of null key (0 or space) */
  20.       char reserve1[4];
  21. };
  22.  
  23.  
  24.  
  25. /*   This Btrieve-required structure holds information about one file. 
  26.  *   It also includes the key structure dimensioned to the maximum 
  27.  *    number of keyed fields within a record.
  28.  */
  29.  
  30. #define MAX_KEYS  12      /* max indices per file */
  31. #define MAX_KEY_SEGS  22  /* max indices (count segments) per file */
  32.  
  33.  
  34. struct filespec               /* file structure specification */
  35. {       int  recl;            /* record length */
  36.         int  page_size;
  37.         int  index_count;     /* number of indexes */
  38.         long tot_recs;        /* total active (non-deleted)
  39.                                  records returned by STATUS operation */
  40.         int  file_flags;      /* a vector of bit flags describing
  41.                                  file attributes (see manual) */
  42.         char reserve2[2];     /* (unused) */
  43.         int  pre_alloc_pages; /* if file is to be pre-allocated assign # 
  44.                                  of pages */
  45.  
  46.  
  47.         /* Note that Btrieve deals with this struct only to this point.
  48.          * We have appended the key structures for our own convenience.
  49.          */
  50.  
  51.  
  52.         struct keyspec keybuf[ MAX_KEY_SEGS ]; /* see above */
  53. };
  54.  
  55.  
  56. /* LISTING 2 */
  57.  
  58. struct btfhand
  59. {    char fname[30];          /* file name */
  60.      char pos_blk[128];       /* position block for Btrieve's use */
  61.      long max_var_recl;       /* longest variable recl allowed */
  62.      char owner[25];          /* file owner name */
  63.      int owner_mode;          /* a vector of bit flags used
  64.                                  to set owner lock, data decryption */
  65. };
  66.  
  67. struct btfkey                      /* key information for one file */
  68. {    int  totsegs;                 /* how many key segments in the file */
  69.      int  num_segs[ MAX_KEYS ];    /* number of segments for this key */
  70.      int  start_seg[ MAX_KEYS ];   /* starting segment for each key */
  71.      char *name[ MAX_KEYS ];       /* what users call this access path */
  72. };
  73.  
  74.  
  75. /* LISTING 3 */
  76.  
  77. /*   ---------- Btrieve loaded into memory -----------
  78. **    Btrieve puts 033H in interrupt vector 7BH when loaded.
  79.  
  80.  
  81. **    Peek at address 7B x 4  = 1ECH
  82. *    -----------------------------------------------------------
  83. */
  84.  
  85.  
  86. void check_bt_loaded()
  87. {
  88.      unsigned resident;
  89.  
  90.      /* Peek at the interrupt vector */
  91.      if (( resident = peekb(0,0x1EC)) != 0x33)
  92.           puts("MEMORY RESIDENT BTRIEVE NEEDS TO BE LOADED." );
  93.      return;
  94. }
  95.  
  96.  
  97. /* LISTING 4 */
  98.  
  99. void btinit0()       /* initialize TABLE file -- file zero */
  100. {
  101.      /* Initialize Btrieve required Structures */
  102.      btstruct[ O ].recl    = 100;         /* init record length */
  103.      btstruct[ 0 ].page_size = 1024;
  104.      btstruct[ 0 ].index_count = 2;       /* not segments */
  105.      btstruct[ 0 ].file_flags = 0;    /* fixed length */
  106.  
  107.  
  108.      /* First Index, first segment */
  109.      btstruct[ TABLEF ].keybuf[0].key_pos = 0;
  110.      btstruct[ TABLEF ].keybuf[0].key_len = 2;
  111.      btstruct[ TABLEF ].keybuf[0].key_flag = MOD | SEG | EXT_TYPE ;
  112.      btstruct[ TABLEF ].keybuf[0].key_type = 1;     /* integer */
  113.  
  114.  
  115.      /*  key is modifiable, segmented (with segments to follow), */
  116.      /*  extend data type is integer */
  117.  
  118.  
  119.      /* First Index, second (and last) segment */
  120.      btstruct[ 0 ].keybuf[1].key_pos  = 50;
  121.      btstruct[ 0 ].keybuf[1].key_len  = 2;
  122.      btstruct[ 0 ].keybuf[1].key_flag = DUP | OK_NULL ;
  123.      btstruct[ 0 ].keybuf[0].key_type = 0;     /* ascii string */
  124.      btstruct[ 0 ].keybuf[1].null_char = 0x20; /* SPACE */
  125.  
  126.  
  127.      /*  key has duplicates, null keys allowed */
  128.      /*  null key is defined as space */
  129.  
  130.  
  131.      /* Initialize other structures as needed */
  132.  
  133.  
  134.      strcpy( fh[0].fname, "TABLE.DAT");  /* assign file name */
  135.      fkey[0].totsegs     = 4;            /* total segments for the file */
  136.      fkey[0].name[0] = "WOOD";           /* name of index 0 */
  137.  
  138.  
  139.      /* continue required initialization */
  140.  
  141.      return;
  142. }
  143.  
  144.  
  145. /* LISTING 5 */
  146.  
  147. /*   Call bt_file() when the file structure is required 
  148.  *      in the data buffer argument.
  149.  *   Operations:  create(14), stat(15)
  150.  */
  151.  
  152.  
  153. int bt_file( opcode, fn )
  154. int opcode;         /* Btrieve operation code */
  155. int fn;             /* file number */
  156. {
  157. struct filespec *fptr = &btstruct[fn];   /* ptr to the file struct */
  158. int buflen=sizeof( struct filespec );
  159. int btstat;
  160. char ext_fname[64];      /* name of file extended across a disk partition */
  161. int in=0;                /* index */
  162.  
  163.  
  164.   if (opcode==B_STAT)  {
  165.      btstat = BTRV(opcode, fh[fn].pos_blk, fptr, &buflen, ext_fname, in);
  166.      if (btstat)  {
  167.           /* error handling */
  168.      }
  169.      else 
  170.         printf("%s%s %ld", "Record count for ",fh[fn].fname, fptr->recl);
  171.   }
  172.  
  173.   else  {
  174.      btstat = BTRV(opcode, fh[fn].pos_blk, fptr, &buflen, fh[fn].fname,in);
  175.  
  176.  
  177.      if ( btstat == 20 )  printf("\nBtrieve needs to be loaded.\n");
  178.   }
  179.   return( btstat );
  180. }
  181. /*    Call bt_openclose() to open or close a Btrieve file.
  182.  *    All of the file locking is handled here.
  183.  *        ---file locking---
  184.  *        -1 = accelerated mode
  185.  *        -2 = user has r/o access, others have normal access.
  186.  *        -4 = exclusive lock - no one else can open it
  187.  *
  188.  *    Operations:  open(0), extend(16), close(1)
  189.  */
  190.  
  191.  
  192. int bt_openclose( opcode, fn, lock )
  193. int opcode;         /* Btrieve operation code */
  194. int fn;             /* our file number */
  195. int lock;           /* locking information switch */
  196. {
  197. int buflen,btstat;
  198.  
  199.  
  200.      buflen = strlen( fh[fn].owner );
  201.  
  202.  
  203.      btstat = BTRV(opcode, fh[fn].pos_blk, fh[fn].owner, &buflen,
  204.                     fh[fn].fname, lock);
  205.  
  206.  
  207.      if (btstat)  {
  208.           /* Place error function call here */
  209.      }
  210.      return( btstat );
  211. }
  212.  
  213.  
  214. /* LISTING 6 */
  215.  
  216. #define bt_open(fn,lock)   bt_openclose(0,fn,lock)
  217. #define bt_close(fn)       bt_openclose(1,fn,0)
  218. #define bt_extend(fn)      bt_openclose(16,fn,0)
  219.  
  220.  
  221. /* LISTING 7 */
  222.  
  223. /*   Call bt_read() when data is to be retrieved.
  224.  *
  225.  *   Operations:
  226.  *   get_pos(22)     Get record position # in databuf
  227.  *   get_direct(23)  Get data by sending the record position
  228.  *                   (returned from get_pos()) in data buffer
  229.  *   get_equal(5)    The key buffer must be padded or
  230.  *                   formatted to match exactly.
  231.  *
  232.  *   version(26), step_direct(24), unlock(27)
  233.  *
  234.  *   These operations will return duplicates:
  235.  *   get_first(12), get_next(6), get_prev(7), get_last(13)
  236.  *
  237.  *   These operations won't return duplicates:
  238.  *   get_greater(8), get_less(10), get_gr_eql(9), get_less_eq(11)
  239.  */
  240.  
  241. int bt_read(op_code, fn, in, databuf, keyval, lock )
  242. int opcode;         /* Btrieve operation code */
  243. int fn;             /* file number */
  244. int in;             /* index number */
  245. char *databuf;      /* data record buffer */
  246. char *keyval;       /* key value */
  247. int lock;           /* application dependant locking # */
  248. {
  249. int buflen;
  250.  
  251.  
  252.      opcode += lock;               /* add optional locking */
  253.      buflen = btstruct[fn].recl;
  254.      btstat = BTRV( opcode, fh[fn].pos_blk, databuf, &buflen, keyval, in);
  255.      if ( btstat )  {
  256.           /* Place error function call here */
  257.      }
  258.      return( btstat ) ;
  259. }
  260.  
  261. /*    Pass bt_write() a valid data record.
  262.  *
  263.  *    Operations:  insert(2), update(3), delete(4), unlock(27)
  264.  */
  265.  
  266.  
  267. int bt_write( opcode, fn, in, databuf, keyval )
  268. int opcode;         /* Btrieve operation code */
  269. int fn;             /* our file number */
  270. int in;             /* number of index to be used for this operation */
  271. char *databuf;      /* data buffer */
  272. char *keyval;       /* key value */
  273. {
  274. int buflen = btstruct[fn].recl;
  275.  
  276.  
  277.      btstat = BTRV(op_code, fh[fn].pos_blk, databuf, &buflen, keyval, in);
  278.  
  279.  
  280.      if ( btstat )  {
  281.         /* Place error function call here. */
  282.      }
  283.      return( btstat ) ;
  284. }
  285.  
  286. /*    Call bt_getkey() for a fast key lookup.
  287.  *
  288.  *    Operations:  Getkey(50+) for opcodes 5-13.
  289.  */
  290.  
  291.  
  292. /* LISTING 8
  293.  
  294. int bt_getkey( op_code, fn, in, keyval )
  295. int op_code, fn, in;        /* as before */
  296. char *keyval;
  297. {
  298. int btstat;
  299. int buflen=4;
  300. char dummybuf[4];
  301.  
  302.  
  303.      btstat = BTRV(op_code, fh[fn].pos_blk, dummybuf, &buflen, keyval, in);
  304.  
  305.  
  306.      if (btstat)  {
  307.                /* Place error function call here. */
  308.      }
  309.      return( btstat ) ;
  310. }
  311. /*   Call bt_spec() as a general purpose function which needs
  312.  *                  and/or returns the key buffer at best.
  313.  *
  314.  *   Operations:  reset(28), stop(25),
  315.  *                begin_trans(19), end_trans(20), abort_trans(21)
  316.  *
  317.  *   get_dir(18)  Directory information is sent or returned
  318.  *   set_dir(17)  in the key buffer for these operations.
  319.  */
  320.  
  321.  
  322. int bt_spec( op_code, fn, in, keyval )
  323. int op_code, fn, in;
  324. char *keyval;
  325. {
  326. int buflen=4;
  327. char dummybuf[4];
  328. int btstat;
  329.  
  330.  
  331.    btstat = BTRV(op_code, fh[fn].pos_blk, dummybuf, &buflen, keyval, in);
  332.    if (btstat)  {
  333.                /* error handling */
  334.    }
  335.    return( btstat ) ;
  336.